package com.example.yijian;

import android.content.Context;
import android.os.Debug;
import android.text.TextUtils;
import android.util.Log;


import androidx.multidex.MultiDex;

import com.example.yijian.base.AndroidOS;
import com.example.yijian.base.BaseApp;
import com.example.yijian.http.HttpConstants;
import com.example.yijian.http.NvsServerClient;
import com.example.yijian.manager.PreferencesManager;
import com.example.yijian.utils.LogUtils;
import com.example.yijian.utils.PathUtils;
import com.example.yijian.utils.TimelineUtil;
import com.example.yijian.utils.asset.NvAssetManager;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.meicam.sdk.NvsStreamingContext;

import java.io.File;


/**
 * @author gexinyu on 2018/5/24.
 */

public class MSApplication extends BaseApp {
    private static String sdkVersion = "";

    public static String getSdkVersion() {
        return sdkVersion;
    }

    public static String getAppId() {
        return getContext().getPackageName();
    }

    private static Context mContext;


    @Override
    public void onCreate() {
        super.onCreate();
        AndroidOS.initConfig(mContext);
        LogUtils.e("MSApplication", "onCreate");
        if (BuildConfig.DEBUG) {
            String traceFileDir = PathUtils.getTraceFileDir();
            if (!TextUtils.isEmpty(traceFileDir)) {
                LogUtils.d("MSApplication=", traceFileDir);
                Debug.startMethodTracing(traceFileDir + "/load.trace");
            }
        }

        /*
         * 初始化
         * initialization
         * */
        mContext = this;
        NvsStreamingContext.setIconSize(1);
        NvsStreamingContext.setMaxReaderCount(4);
        String licensePath = "assets:/meishesdk.lic";
        TimelineUtil.initStreamingContext(getContext(), licensePath);
        TimelineUtil.initEffectSdkContext(getContext(), licensePath);

        NvAssetManager.init(getContext());

        NvsStreamingContext instance = NvsStreamingContext.getInstance();
        if (instance != null) {
            NvsStreamingContext.SdkVersion version = instance.getSdkVersion();
            if (version != null) {
                MSApplication.sdkVersion = version.majorVersion + "." + version.minorVersion + "." + version.revisionNumber;
            }
            //HDR亮度调节 增亮
            instance.setColorGainForSDRToHDR(2);
        }

        NvsServerClient.get().initConfig(this, HttpConstants.HOST_HTTPS + "://" + HttpConstants.HOST);
//        if (BuildConfig.DEBUG) {
//            NvsServerClient.get().initConfig(this, HttpConstants.HOST_HTTP + "://" + HttpConstants.HOST_DEBUG);
//        } else {
//            NvsServerClient.get().initConfig(this, HttpConstants.HOST_HTTPS + "://" + HttpConstants.HOST);
//        }
        PreferencesManager.get().init(this);
        String crashFilePath = PathUtils.getFolderDirPath( File.separator + "crash" );
        Log.e("MSApplication","crashFilePath:"+crashFilePath);
        // 手机crash日志的路径：/storage/emulated/0/Android/data/com.meishe.ms106sdkdemo/files/crash
        if (BuildConfig.DEBUG) {
            Debug.stopMethodTracing();
        }
        Fresco.initialize(this);
    }

    public static Context getAppContext() {
        return mContext;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

}
